﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using VietPay.KooBoo.Data.Infrastructure;
using VietPay.KooBoo.Business.Models;
using VietPay.KooBoo.Business.Interfaces;
using VietPay.KooBoo.Data.Interfaces;
using VietPay.KooBoo.Data;
using VietPay.KooBoo.Data.EntityModels;

namespace VietPay.KooBoo.Business
{
    public class StockBalanceService : ServiceBase<IStockBalanceModel>, IStockBalanceService
    {
        public StockBalanceService()
            : this(new StockBalanceRepository())
        {
        }

        private IStockBalanceRepository _repository;
        

        public StockBalanceService(IStockBalanceRepository newsRepository)
        {
            _repository = newsRepository ?? new StockBalanceRepository();
        }

        protected override Type LogPrefix
        {
            get { return this.GetType(); }
        }

        public StockBalanceListViewModel GetStockBalanceProductList(int distributorID, int PageSize, int CurrentPage)
        {
            try
            {
                if ((CurrentPage == 0) || (PageSize == 0))
                    return null;

                var vms = new VMS2_DistributorEntities();

                var result = (from a in vms.StockBalances
                              join p in vms.Products on a.ProductID equals p.ProductID
                              join c in vms.Categories on p.CategoryID equals c.CategoryID
                              where a.DistributorID == distributorID
                              select new StockBalanceProductViewModel
                              {
                                  BalanceID=a.BalanceID,
                                  CategoryName=c.CategoryName,
                                  ProductName=p.ProductName,
                                  Quantity=a.Quantity
                              }).OrderBy(p => p.CategoryName);

                StockBalanceListViewModel model = new StockBalanceListViewModel();
                model.StockBalanceProductList = result.ToList<StockBalanceProductViewModel>();
                return model;
            }
            catch (System.Exception e)
            {
                this.LogError("Error getting the StockBalance list", e);
                return null;
            }
        }


        public StockBalanceViewModel GetSingle(Expression<Func<StockBalanceViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public void Add(StockBalanceViewModel vModel)
        {
            StockBalance entity=new StockBalance();
            _repository.Add(entity);
        }

        public void Delete(StockBalanceViewModel vModel)
        {
            //StockBalance news = _repository.GetAll(p => p.ProductID== vModel.ProductID).FirstOrDefault();
            //_repository.Delete(news);
        }

        public void Update(StockBalanceViewModel vModel)
        {
            //StockBalance entity = _repository.GetAll(p => p.ProductID == vModel.ProductID).FirstOrDefault();

            StockBalance entity = new StockBalance();
            _repository.Attach(entity);
        }

        public IList<StockBalanceViewModel> GetAll(Expression<Func<StockBalanceViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public IList<StockBalanceViewModel> GetAll()
        {
            throw new NotImplementedException();
        }

        public IQueryable<StockBalanceViewModel> Query(Expression<Func<StockBalanceViewModel, bool>> whereCondition)
        {
            List<StockBalanceViewModel> newsQuery = new List<StockBalanceViewModel>();
            IQueryable<StockBalance> query = _repository.GetQueryable();
            return newsQuery.AsQueryable();
        }

        public long Count(Expression<Func<StockBalanceViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public long Count()
        {
            throw new NotImplementedException();
        }
    }
}
